// CubeModel 继承自 ContentData
import 'package:cbec_app/entity/home/content/content_data.dart';

class CubeModel extends ContentData {
  int? layout;
  int? borderWidth;
  int? showMethod;
  int? pageMargin;
  List<CubeSubEntryModel>? subEntry;

  CubeModel({
    this.layout,
    this.borderWidth,
    this.showMethod,
    this.pageMargin,
    this.subEntry,
  }) {
    type = 'cube'; // 设置 type 属性
  }

  // 从 JSON 创建实例

  factory CubeModel.fromJson(Map<String, dynamic> json) {
    return CubeModel(
      layout: json['layout'] ?? 0,
      borderWidth: json['border_width'] ?? 0,
      showMethod: json['show_method'] ?? 0,
      pageMargin: json['page_margin'] ?? 0,
      subEntry: (json['sub_entry'] as List).map((item) => CubeSubEntryModel().fromJson(item)).toList(),
    );
  }

  @override
  ContentData fromJson(Map<String, dynamic> json) {
    throw CubeModel.fromJson(json);
  }

  // 转换为 JSON
  @override
  Map<String, dynamic> toJson() {
    return {
      'layout': layout,
      'border_width': borderWidth,
      'show_method': showMethod,
      'page_margin': pageMargin,
      'sub_entry': subEntry?.map((item) => item.toJson()).toList(),
      'type': type,
    };
  }
}

// SubEntryModel 类
class CubeSubEntryModel {
  String? linkType;
  int? imageHeight;
  String? imageUrl;
  String? linkTitle;
  String? linkId;
  int? imageWidth;

  CubeSubEntryModel({
    this.linkType,
    this.imageHeight,
    this.imageUrl,
    this.linkTitle,
    this.linkId,
    this.imageWidth,
  });

  // 从 JSON 创建实例
  CubeSubEntryModel fromJson(Map<String, dynamic> json) {
    return CubeSubEntryModel(
      linkType: json['link_type'] ?? '',
      imageHeight: json['image_height'] ?? 0,
      imageUrl: json['image_url'] ?? '',
      linkTitle: json['link_title'] ?? '',
      linkId: json['link_id']?.toString() ?? '', // 确保转换为字符串
      imageWidth: json['image_width'] ?? 0,
    );
  }

  // 转换为 JSON
  Map<String, dynamic> toJson() {
    return {
      'link_type': linkType,
      'image_height': imageHeight,
      'image_url': imageUrl,
      'link_title': linkTitle,
      'link_id': linkId,
      'image_width': imageWidth,
    };
  }
}
